package com.boe.common.util.freemark;

import lombok.*;
import com.boe.common.exception.ParameterInvalidException;
import freemarker.ext.beans.StringModel;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public abstract class BaseMethodModelFreemarker implements TemplateMethodModelEx {
    public <T> List<T> getFreemarkerParameterList(T... list) {
        return Arrays.asList(list).stream().map(it -> (T) getFreemarkerParameter(it)).collect(Collectors.toList());
    }

    public Object getFreemarkerParameter(Object p1) {
        if (p1 == null) {
            return null;
        }

        Object paramValue = p1;
        if (p1 instanceof StringModel) {
            paramValue = ((StringModel) p1).getWrappedObject();
        } else if (p1 instanceof SimpleScalar) {
            paramValue = ((SimpleScalar) p1).getAsString();
        }


        return paramValue;
    }
}